/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.wiztools.xpathevaluator;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author subwiz
 */
public class MessageDialog extends JDialog {
    
    private JTextArea jta = new JTextArea(25, 30);
    private JScrollPane jsp = new JScrollPane(jta);
    
    private MessageDialog me;
    private JFrame frame;

    public MessageDialog(JFrame frame){
        super(frame, true); // true -> is modal
        this.frame = frame;
        me = this;
        init();
    }
    
    private void init(){
        setTitle("Error trace");
        
        Container panel = this.getContentPane();
        panel.setLayout(new BorderLayout());
        
        jta.setEditable(false);
        panel.add(jsp, BorderLayout.CENTER);
        
        JButton jb = new JButton("Ok");
        jb.setMnemonic('o');
        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                me.setVisible(false);
            }
        });
        
        JPanel jp_south = new JPanel();
        jp_south.setLayout(new FlowLayout(FlowLayout.CENTER));
        jp_south.add(jb);
        panel.add(jp_south, BorderLayout.SOUTH);
        
        this.pack();
    }
    
    public void showMessage(String msg){
        Dimension d = jsp.getPreferredSize();
        jta.setText(msg);
        jta.setCaretPosition(0);
        jsp.setPreferredSize(d);
        this.setLocationRelativeTo(frame);
        this.setVisible(true);
    }

    @Override
    public void setVisible(boolean b) {
        if(b == false){
            jta.setText("");
        }
        super.setVisible(b);
    }
    
}
